<?php
// 应用公共文件
if (!function_exists('md5Pass')) {
    function md5Pass($oldpass, $salt)
    {
        return md5(md5($oldpass) . $salt);
    }
}

if (!function_exists('randomSalt')) {
    function randomSalt($len = 6)
    {
        $str = str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
        return substr($str, 0 ,$len);
    }
}

if (!function_exists('buildOptions')) {
    // 递归构建带符号的选项
    function buildOptions($nodes, $level = 0, $isLast = true)
    {
        $options = [];
        $prefix = $level === 0 ? '' : ($isLast ? '    ' : '│   '); // 连接线

        foreach ($nodes as $key => $node) {
            $isLeaf = empty($node['children']);
            $connector = $isLast ? '└─ ' : '├─ '; // 分支符号

            $options[] = [
                'id' => $node['id'],
                'title' => $prefix . $connector . $node['title']
            ];

            // 递归处理子节点
            if (!empty($node['children'])) {
                $childIsLast = $key == array_key_last($nodes);
                $childOptions = buildOptions(
                    $node['children'],
                    $level + 1,
                    $childIsLast
                );
                $options = array_merge($options, $childOptions);
            }
        }
        return $options;
    }
}